﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace DalatCooking.Core.Helper
{
    public class StringHelper
    {
        // Ham kiem tra du lieu bang null hoac rong
        public static bool IsNullorEmpty(string text)
        {
            return text == null || text.Trim() == string.Empty;
        }
        // Ham tao mat khau ngau nhien
        public static string RandomString(int len)
        {
            string allowedChars = "";
            allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";
            allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";
            allowedChars += "1,2,3,4,5,6,7,8,9,0,!,@,#,$,%,&,?";

            char[] sep = { ',' };
            string[] arr = allowedChars.Split(sep);
            string randomString = "";
            string temp = "";
            Random rand = new Random();
            for (int i = 0; i < len; i++)
            {
                temp = arr[rand.Next(0, arr.Length)];
                randomString += temp;
            }
            return randomString;
        }      

        public static string TruncateString(string str, int maxLength)
        {
            if (string.IsNullOrEmpty(str) || maxLength <= 0)
                return string.Empty;
            else if (str.Length > maxLength)
                return str.Substring(0, maxLength) + "...";
            else
                return str;
        }
         public static string TruncateDescription(string str, int maxLength)
        {
            string plainText = StripTagsRegexCompiled(str);
            if (string.IsNullOrEmpty(plainText) || maxLength <= 0)
                return string.Empty;
            else if (plainText.Length > maxLength)
                return plainText.Substring(0, maxLength) + "...";
            else
                return plainText;
        }
        
        /// <summary>
        /// Ham thay the gia tri trong chuon bang chuoi gia tri khac
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="oldValue"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        public static string ReplaceMyString(string strSource, string oldValue, string newValue)
        {
            string temp = "";
            if (strSource.Length == 0)
                return strSource;
            else
                if (strSource.Length > 0)
                {
                    temp = strSource.Replace(oldValue, newValue);
                }
            return temp;
        }

        /// <summary>
        /// Ham cat chuoi. Lay chuoi nam giua 2 gia tri.
        /// Ex: CutSubstringInContent("A", "B","A Chuoi Can Cat B") -> Result: Chuoi Can Cat
        /// </summary>
        /// <param name="subFrom"></param>
        /// <param name="subTo"></param>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static string CutSubstringInContent(string subFrom, string subTo, string strSource)
        {
            string Cutstr = "";
            int pos1 = strSource.IndexOf(subFrom);
            if (pos1 < 0 || pos1 > strSource.Length)
            {
                return Cutstr.Trim().ToUpper();
            }
            int pos2 = strSource.IndexOf(subTo);
            if (pos2 < 0 || pos2 > strSource.Length)
            {
                return Cutstr.Trim().ToUpper();
            }
            if (pos1 - pos2 == 0)
            {
                return Cutstr.Trim().ToUpper();
            }
            if (pos1 > pos2)
            {
                int pos3 = 0;
                pos3 = pos1; pos1 = pos2; pos2 = pos3;
            }
            Cutstr = strSource.Substring(pos1 + subFrom.Length, strSource.Length - ((strSource.Length - pos2) + (pos1 + subFrom.Length)));
            return Cutstr.Trim().ToUpper();
        }
        
        /// <summary>
        /// Ham xoa dau Unicode
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static string RemoveMark1(string keyword)
        {
            string result = "";
            for (int i = 0; i < keyword.Length; i++)
            {
                switch (keyword[i])
                {
                    case 'á':
                        result += "a";
                        break;
                    case 'Á':
                        result += "A";
                        break;
                    case 'à':
                        result += "a";
                        break;
                    case 'À':
                        result += "A";
                        break;
                    case 'ã':
                        result += "a";
                        break;
                    case 'Ã':
                        result += "A";
                        break;
                    case 'ả':
                        result += "a";
                        break;
                    case 'Ả':
                        result += "A";
                        break;
                    case 'ạ':
                        result += "a";
                        break;
                    case 'Ạ':
                        result += "A";
                        break;
                    case 'ă':
                        result += "a";
                        break;
                    case 'Ă':
                        result += "A";
                        break;
                    case 'ằ':
                        result += "a";
                        break;
                    case 'Ằ':
                        result += "A";
                        break;
                    case 'ắ':
                        result += "a";
                        break;
                    case 'Ắ':
                        result += "A";
                        break;
                    case 'ặ':
                        result += "a";
                        break;
                    case 'Ặ':
                        result += "A";
                        break;
                    case 'ẵ':
                        result += "a";
                        break;
                    case 'Ẵ':
                        result += "A";
                        break;
                    case 'ẳ':
                        result += "a";
                        break;
                    case 'Ẳ':
                        result += "A";
                        break;
                    case 'â':
                        result += "a";
                        break;
                    case 'Â':
                        result += "A";
                        break;
                    case 'ấ':
                        result += "a";
                        break;
                    case 'Ấ':
                        result += "A";
                        break;
                    case 'ầ':
                        result += "a";
                        break;
                    case 'Ầ':
                        result += "A";
                        break;
                    case 'ẫ':
                        result += "a";
                        break;
                    case 'Ẫ':
                        result += "A";
                        break;
                    case 'ẩ':
                        result += "a";
                        break;
                    case 'Ẩ':
                        result += "A";
                        break;
                    case 'ậ':
                        result += "a";
                        break;
                    case 'Ậ':
                        result += "A";
                        break;
                    case 'ê':
                        result += "e";
                        break;
                    case 'Ê':
                        result += "E";
                        break;
                    case 'ế':
                        result += "e";
                        break;
                    case 'Ế':
                        result += "E";
                        break;
                    case 'ề':
                        result += "e";
                        break;
                    case 'Ề':
                        result += "E";
                        break;
                    case 'ễ':
                        result += "e";
                        break;
                    case 'Ễ':
                        result += "E";
                        break;
                    case 'ể':
                        result += "e";
                        break;
                    case 'Ể':
                        result += "E";
                        break;
                    case 'ệ':
                        result += "e";
                        break;
                    case 'Ệ':
                        result += "E";
                        break;
                    case 'é':
                        result += "e";
                        break;
                    case 'É':
                        result += "E";
                        break;
                    case 'è':
                        result += "e";
                        break;
                    case 'È':
                        result += "E";
                        break;
                    case 'ẻ':
                        result += "e";
                        break;
                    case 'Ẻ':
                        result += "E";
                        break;
                    case 'ẽ':
                        result += "e";
                        break;
                    case 'Ẽ':
                        result += "E";
                        break;
                    case 'ẹ':
                        result += "e";
                        break;
                    case 'Ẹ':
                        result += "E";
                        break;
                    case 'ò':
                        result += "o";
                        break;
                    case 'Ò':
                        result += "O";
                        break;
                    case 'ó':
                        result += "o";
                        break;
                    case 'Ó':
                        result += "O";
                        break;
                    case 'ọ':
                        result += "o";
                        break;
                    case 'Ọ':
                        result += "O";
                        break;
                    case 'õ':
                        result += "o";
                        break;
                    case 'Õ':
                        result += "O";
                        break;
                    case 'ỏ':
                        result += "o";
                        break;
                    case 'Ỏ':
                        result += "O";
                        break;
                    case 'ô':
                        result += "o";
                        break;
                    case 'Ô':
                        result += "O";
                        break;
                    case 'ồ':
                        result += "o";
                        break;
                    case 'Ồ':
                        result += "O";
                        break;
                    case 'ố':
                        result += "o";
                        break;
                    case 'Ố':
                        result += "O";
                        break;
                    case 'ổ':
                        result += "o";
                        break;
                    case 'Ổ':
                        result += "O";
                        break;
                    case 'ỗ':
                        result += "o";
                        break;
                    case 'ộ':
                        result += "o";
                        break;
                    case 'Ộ':
                        result += "O";
                        break;
                    case 'ù':
                        result += "u";
                        break;
                    case 'Ù':
                        result += "U";
                        break;
                    case 'ú':
                        result += "u";
                        break;
                    case 'Ú':
                        result += "U";
                        break;
                    case 'ụ':
                        result += "u";
                        break;
                    case 'Ụ':
                        result += "U";
                        break;
                    case 'ũ':
                        result += "u";
                        break;
                    case 'Ũ':
                        result += "U";
                        break;
                    case 'ủ':
                        result += "u";
                        break;
                    case 'Ủ':
                        result += "U";
                        break;
                    case 'ư':
                        result += "u";
                        break;
                    case 'Ư':
                        result += "U";
                        break;
                    case 'ừ':
                        result += "u";
                        break;
                    case 'Ừ':
                        result += "U";
                        break;
                    case 'ứ':
                        result += "u";
                        break;
                    case 'Ứ':
                        result += "U";
                        break;
                    case 'ử':
                        result += "u";
                        break;
                    case 'Ử':
                        result += "U";
                        break;
                    case 'ữ':
                        result += "u";
                        break;
                    case 'Ữ':
                        result += "U";
                        break;
                    case 'ự':
                        result += "u";
                        break;
                    case 'Ự':
                        result += "U";
                        break;
                    case 'ì':
                        result += "i";
                        break;
                    case 'Ì':
                        result += "I";
                        break;
                    case 'í':
                        result += "i";
                        break;
                    case 'Í':
                        result += "I";
                        break;
                    case 'ỉ':
                        result += "i";
                        break;
                    case 'Ỉ':
                        result += "I";
                        break;
                    case 'ĩ':
                        result += "i";
                        break;
                    case 'Ĩ':
                        result += "I";
                        break;
                    case 'ị':
                        result += "i";
                        break;
                    case 'Ị':
                        result += "I";
                        break;
                    case 'ỳ':
                        result += "y";
                        break;
                    case 'Ỳ':
                        result += "Y";
                        break;
                    case 'ý':
                        result += "y";
                        break;
                    case 'Ý':
                        result += "Y";
                        break;
                    case 'ỷ':
                        result += "y";
                        break;
                    case 'Ỷ':
                        result += "Y";
                        break;
                    case 'ỹ':
                        result += "y";
                        break;
                    case 'Ỹ':
                        result += "Y";
                        break;
                    case 'đ':
                        result += "d";
                        break;
                    case 'Đ':
                        result += 'D';
                        break;

                    default:
                        result += keyword[i].ToString();
                        break;
                }
            }
            return result;
        }
        /// <summary>
        /// Ham xoa khoang trang trong chuoi
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RemoveWhitespace(string input)
        {
            string strPattern = @"[\s]+";
            Regex RegExp = new Regex(strPattern);
            return RegExp.Replace(input, "").Trim();
        }
        private static string[] VietNamChar =
             { 
           "aAeEoOuUiIdDyY", 
           "áàạảãâấầậẩẫăắằặẳẵ", 
           "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", 
           "éèẹẻẽêếềệểễ", 
           "ÉÈẸẺẼÊẾỀỆỂỄ", 
           "óòọỏõôốồộổỗơớờợởỡ", 
           "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", 
           "úùụủũưứừựửữ", 
           "ÚÙỤỦŨƯỨỪỰỬỮ", 
           "íìịỉĩ", 
           "ÍÌỊỈĨ", 
           "đ", 
           "Đ", 
           "ýỳỵỷỹ", 
           "ÝỲỴỶỸ"        
             };
        public static string ReplaceUnicode(string strInput)
        {
            for (int i = 1; i < VietNamChar.Length; i++)
            {
                for (int j = 0; j < VietNamChar[i].Length; j++)
                {
                    strInput = strInput.Replace(VietNamChar[i][j], VietNamChar[0][i - 1]);
                }
            }
            return strInput;
        }

        public static String RemoveDiacritics(String s)
        {
            String normalizedString = s.Normalize(NormalizationForm.FormD);
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < normalizedString.Length; i++)
            {
                Char c = normalizedString[i];
                if (System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c) != System.Globalization.UnicodeCategory.NonSpacingMark)
                    stringBuilder.Append(c);
            }

            return stringBuilder.ToString();
        }
        public static string RemoveDiacriticsURL(string s)
        {
            string str = RemoveDiacritics(s).ToLower();
           // return str.Replace(" ", "-");
            string strPattern = @"[\s\:\'\""\,\+\&\?\\\!]+";
            Regex RegExp = new Regex(strPattern);
            return RegExp.Replace(str, "-").Trim();
        }

        /// <summary>
        /// Remove HTML from string with Regex.
        /// </summary>
        public static string StripTagsRegex(string source)
        {
            return Regex.Replace(source, "<.*?>", string.Empty);
        }

        /// <summary>
        /// Compiled regular expression for performance.
        /// </summary>
        static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);

        /// <summary>
        /// Remove HTML from string with compiled Regex.
        /// </summary>
        public static string StripTagsRegexCompiled(string source)
        {
            return _htmlRegex.Replace(source, string.Empty);
        }

        /// <summary>
        /// Remove HTML tags from string using char array.
        /// </summary>
        public static string StripTagsCharArray(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }
        public static string ReplaceNvarchar2String(string input)
        {
            //string strPattern = @"[\s]+";   
            string str = "";
            switch (input.ToLower())
            {
                default:
                    str = input;
                    break;
                case "binary":
                    str = "bit";
                    break;
                case "bit":
                    str = "bool";
                    break;
                case "char":
                    str = "string";
                    break;
                case "datetime":
                    str = "DateTime";
                    break;
                case "decimal":
                    str = "int";
                    break;
                //case "float":
                //    break;
                case "image":
                    str = "bit";
                    break;
                //case "int":
                //    break;
                case "money":
                    str = "double"; // float
                    break;
                case "nchar":
                    str = "string";
                    break;
                case "ntext":
                    str = "string";
                    break;
                case "nvarchar":
                    str = "string";
                    break;
                case "real":
                    break;
                case "smalldatetime":
                    str = "DateTime";
                    break;
                case "smallint":
                    str = "int";
                    break;
                case "smallmoney":
                    str = "double"; // float
                    break;
                case "text":
                    str = "string";
                    break;
                case "tinyint":
                    str = "int";
                    break;
                case "varbinary":
                    str = "bit";
                    break;
                case "varchar":
                    str = "string";
                    break;
                case "uniqueidentifier":
                    str = "string";
                    break;
            }
            return str;
        }
    }
}